package com.game.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * 设备信息获取类
 */
public class ApplicationUtil {
    protected static String TAG = "ApplicationUtil";
    @SuppressLint("StaticFieldLeak")
    protected static Context context = null;
    /**
     * 设备id
     */
    protected static String  devicesID = null;
    /**
     * app版本
     */
    protected static String  appVersion = null;
    /**
     * 初始化
     */
    public static void Init(Context context)
    {
        ApplicationUtil.context = context;
        //获取设备id
        ApplicationUtil.devicesID = ApplicationUtil.getMD5(ApplicationUtil.getDeviceIdentifier()) ;
        //获取设备版本
        ApplicationUtil.appVersion =ApplicationUtil.getApplicationVersion();
        Log.i(TAG,devicesID);
        Log.i(TAG,appVersion);
    }

    /**
     * 获取app版本号
     * @return String
     */
    public static String getAppVersion()
    {
        return ApplicationUtil.appVersion;
    }

    /**
     * 获取设备id
     * @return String
     */
    public static String getDeviceId()
    {
        return ApplicationUtil.devicesID;
    }


    /**
     *获取app版本
     * @return String
     */
    protected static String getApplicationVersion() {
        PackageInfo info = null;
        try {
            info = ApplicationUtil.context.getPackageManager()
                    .getPackageInfo(ApplicationUtil.context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        return info.versionName;
    }

    /**
     * 获取设备码
     * @return String
     */
    protected static String getDeviceIdentifier() {
        String ID = ApplicationUtil.getGPS_ADID();
        if(ID==null)
        {
            ID = ApplicationUtil.getAndroidID();
        }
        if(ID==null)
        {
            ID = ApplicationUtil.getPseudoID();
        }
        return ID;
    }


    /**
     * 通过取出ROM版本、制造商、CPU型号以及其他硬件信息来伪造设备标识
     *
     * @return 伪造的设备标识，不会为空，但会有一定的概率出现重复
     */
    protected static String getPseudoID() {
        final int MODULUS = 10;
        StringBuilder sb = new StringBuilder();
        sb.append(Build.BOARD.length() % MODULUS);
        sb.append(Arrays.deepToString(Build.SUPPORTED_ABIS).length() % MODULUS);
        sb.append(Build.DEVICE.length() % MODULUS);
        sb.append(Build.DISPLAY.length() % MODULUS);
        sb.append(Build.HOST.length() % MODULUS);
        sb.append(Build.ID.length() % MODULUS);
        sb.append(Build.MANUFACTURER.length() % MODULUS);
        sb.append(Build.BRAND.length() % MODULUS);
        sb.append(Build.MODEL.length() % MODULUS);
        sb.append(Build.PRODUCT.length() % MODULUS);
        sb.append(Build.BOOTLOADER.length() % MODULUS);
        sb.append(Build.HARDWARE.length() % MODULUS);
        sb.append(Build.TAGS.length() % MODULUS);
        sb.append(Build.TYPE.length() % MODULUS);
        sb.append(Build.USER.length() % MODULUS);
        return sb.toString();
    }

    /**
     * 获取AndroidID
     * @return String
     */
    protected  static String getAndroidID()
    {
        String ID = android.provider.Settings.Secure.getString(
                ApplicationUtil.context.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);
        return ID;
    }
    /**
     *获取谷歌广告id
     * @return String
     */
    protected static String getGPS_ADID() {
        String adsId = null;
        try{
            AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(ApplicationUtil.context);
            adsId = adInfo.getId();
        }
        catch (Exception e)
        {
            Log.i(ApplicationUtil.TAG,"getGPS_ADID error");
        }
        return adsId;
    }

    protected static String getMD5(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] hash = digest.digest(input.getBytes());

            StringBuilder hexString = new StringBuilder();

            for (byte b : hash) {
                String hex = Integer.toHexString(0xFF & b);
                if (hex.length() == 1) {
                    hexString.append("0");
                }
                hexString.append(hex);
            }

            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}
